/*
 * All rights Reserved, Designed By DataDriver
 * Copyright:    DataDriver.Inc
 * Company:      Zhuo Wo Infomation Technology (ShangHai) CO.LTD
 */
package com.sun.pc;

/**
 * 生产者和消费者问题:
 *
 * 多线程操作资源类时会出现混乱的现象：那么这个问题改则呢么解决呢？
 * 1、传统的Synchronized锁
 *
 */
public class A {

    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(()->{
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        },"A").start();
        new Thread(()->{  for (int i = 0; i < 40; i++) {
            ticket.sale();
        }},"B").start();
        new Thread(()->{  for (int i = 0; i < 40; i++) {
            ticket.sale();
        }},"C").start();
    }

}


/**
 * 线程操作资源类
 * <p>
 * 属性、方法
 */
class Ticket {

    private int num = 30;

    public void sale() {
        if (num != 0) {
            System.out.println(Thread.currentThread().getName()+ "卖出票为:" + num-- + "剩余票:" + num);
        }
    }
}
